
import Crypto from './crypto';
/*
     * 写入
     * @expire 时间戳(毫秒)
     */
const set=(key, value, expire)=> {
    let val=Crypto.encrypt({
        value: value,
        time: +new Date,
        expire: expire
    });
    uni.setStorageSync(key.toString(), val);
}
/*
     * 读取
     * @return isExpired ==> 是否过期
     */
const read=(key)=> {
    let data = uni.getStorageSync(key.toString());
    if (data) {
        data = Crypto.decrypt(data);
        if (data.expire && (data.expire <= new Date().getTime() - data.time)) {
            uni.removeStorageSync(key.toString());
            return null;
        } else {
            return data.value;
        };
    };
    return null;
}

 /*
     * 删除
     * @key 不传则删除所有
     */
 const clear=(key)=> {
    if (key) {
        
        uni.removeStorageSync(key.toString());
    } else {
        uni.clearStorageSync();
    };
}

export default{
    set,
    read,
    clear
}